<?php

namespace App\Http\Controllers\Admin;

use App\Models\ExpressFeeConfig;
use Illuminate\Http\Request;

class ExpressFeeConfigController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->middleware('check_admin_login');
    }

    public function store(Request $request)
    {
        $unfar_express_fee = intval($request->get('unfar_express_fee'));  //非偏远地区 运费
        $unfar_order_price = intval($request->get('unfar_order_price'));  //非偏远地区 买书订单满多少可免运费
        $far_express_fee   = intval($request->get('far_express_fee'));   //偏远地区 运费
        $far_order_price   = intval($request->get('far_order_price'));    //偏远地区 买书订单满多少可免运费


        $data1 = [
            'express_fee' => $unfar_express_fee,
            'order_price' => $unfar_order_price,
        ];

        $data2 = [
            'express_fee' => $far_express_fee,
            'order_price' => $far_order_price,
        ];

        $unfar_info = ExpressFeeConfig::query()->where('type',1)->first();
        if(empty($unfar_info))
        {
            $data1['type']       = 1;
            $data1['created_at'] = $this->time;

            ExpressFeeConfig::create($data1);
        }
        else
        {
            $data1['updated_at'] = $this->time;
            $unfar_info->update($data1);
        }

        $far_info = ExpressFeeConfig::query()->where('type',2)->first();
        if(empty($far_info))
        {
            $data2['type']       = 2;
            $data2['created_at'] = $this->time;

            ExpressFeeConfig::create($data2);
        }
        else
        {
            $data2['updated_at'] = $this->time;
            $far_info->update($data2);
        }

        return $this->ok([],'提交成功');
    }

    public function show(Request $request)
    {
        $unfar_express_fee = 0;
        $unfar_order_price = 0;
        $unfar_info = ExpressFeeConfig::query()->where('type',1)->first();
        if(!empty($unfar_info))
        {
            $unfar_express_fee = $unfar_info->express_fee;
            $unfar_order_price = $unfar_info->order_price;
        }

        $far_express_fee = 0;
        $far_order_price = 0;
        $far_info = ExpressFeeConfig::query()->where('type',2)->first();
        if(!empty($far_info))
        {
            $far_express_fee = $far_info->express_fee;
            $far_order_price = $far_info->order_price;;
        }

        $result = [
            'unfar_express_fee' => $unfar_express_fee,
            'unfar_order_price' => $unfar_order_price,
            'far_express_fee' => $far_express_fee,
            'far_order_price' => $far_order_price,
        ];
        return $this->ok($result);
    }
}
